package com.qf.flash.ui.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qf.flash.R;
import com.qf.flash.model.AlipayInfoBean;
import com.qf.flash.mvp.contract.BindAlipayContract;
import com.qf.flash.mvp.presenter.BindAlipayPresenter;
import com.qf.flash.mvp.presenter.VerCodePresenter;
import com.qf.flash.utils.AppToast;
import com.qf.flash.utils.MyUtil;
import com.qf.flash.widget.ClearEditText;
import com.qf.flashlib.base.BaseActivity;
import com.qf.flashlib.exception.ApiException;
import com.qf.flashlib.util.RxUtils;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

import static com.qf.flash.constant.FlashCodes.VER_CODE_ALIPAY;

public class BindAlipayActivity extends BaseActivity implements BindAlipayContract.View {

    @BindView(R.id.tv_top_back)
    TextView tvTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.et_alipay_account)
    ClearEditText etAlipayAccount;
    @BindView(R.id.et_phone)
    ClearEditText etPhone;
    @BindView(R.id.tv_getCode)
    TextView tvGetCode;
    @BindView(R.id.et_fill_yzm)
    ClearEditText etFillYzm;
    @BindView(R.id.et_name)
    ClearEditText etName;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.ll_yzm)
    LinearLayout llYzm;
    @BindView(R.id.ll_pwd)
    LinearLayout llPwd;
    @BindView(R.id.ll_name)
    LinearLayout ll_name;
    @BindView(R.id.tv_alipay_account)
    TextView tvAlipayAccount;
    private BindAlipayPresenter bindAlipayPresenter;
    private VerCodePresenter codePresenter;
    private AlipayInfoBean alipayInfoBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bind_zfb;
    }

    @Override
    protected void initInject() {
        super.initInject();
        bindAlipayPresenter = new BindAlipayPresenter();
        codePresenter = new VerCodePresenter();
        initPresenters(bindAlipayPresenter, codePresenter);
    }

    @Override
    protected void initViewAndData() {
        tvTopTitle.setText("绑定支付宝");
        alipayInfoBean = (AlipayInfoBean) getIntent().getSerializableExtra("alipay");
        if (alipayInfoBean != null && !MyUtil.isNullOrEmpty(alipayInfoBean.getId())) {
            etAlipayAccount.setVisibility(View.GONE);
            tvAlipayAccount.setVisibility(View.VISIBLE);
            tvAlipayAccount.setText(alipayInfoBean.getBankCard());
            llYzm.setVisibility(View.INVISIBLE);
            llPwd.setVisibility(View.INVISIBLE);
            ll_name.setVisibility(View.GONE);
            tvSure.setText("解除绑定");
            etAlipayAccount.setClearIconVisible(false);
        } else {
            etAlipayAccount.setVisibility(View.VISIBLE);
            tvAlipayAccount.setVisibility(View.GONE);
            etAlipayAccount.requestFocus();
        }
    }

    public static void start(Activity activity, AlipayInfoBean alipayInfoBean) {
        Intent intent = new Intent(activity, BindAlipayActivity.class);
        intent.putExtra("alipay", alipayInfoBean);
        activity.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_top_back, R.id.tv_getCode, R.id.tv_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_top_back:
                finish();
                break;
            case R.id.tv_getCode:
                if (MyUtil.isPhone(etPhone.getText().toString().trim())) {
                    if (!isCounting) {
                        startCounting();
                        codePresenter.getVerCode(etPhone.getText().toString().trim(), VER_CODE_ALIPAY);
                    }

                } else {
                    AppToast.showToast("手机号码有误！请填写正确手机号！");
                    AppToast.shake(etPhone);
                }
                break;
            case R.id.tv_sure:
                if (alipayInfoBean != null && !MyUtil.isNullOrEmpty(alipayInfoBean.getId())) {
                    tvTopTitle.setText("解除绑定");
                    tvSure.setText("确定解除");
                    llYzm.setVisibility(View.VISIBLE);
                    llPwd.setVisibility(View.VISIBLE);
                    if (!MyUtil.isNullOrEmpty(etFillYzm.getText().toString()) && MyUtil.isPhone(etPhone.getText().toString())) {
                        bindAlipayPresenter.unBindAlipay(alipayInfoBean.getId(), etFillYzm.getText().toString());
                    }
                } else {
                    if (!MyUtil.isNullOrEmpty(etAlipayAccount.getText().toString())
                            && !MyUtil.isNullOrEmpty(etFillYzm.getText().toString())
                            && MyUtil.isPhone(etPhone.getText().toString())
                            && !TextUtils.isEmpty(etName.getText().toString())) {
                        bindAlipayPresenter.bindAlipay(etPhone.getText().toString(), etAlipayAccount.getText().toString(),etName.getText().toString(), etFillYzm.getText().toString());
                    } else {
                        AppToast.showToast("信息填写有误！请检查！");
                    }
                }
                break;
        }
    }

    @Override
    public void finished() {
        finish();
    }


    private int interval = 50; //读秒时间
    private boolean isCounting;
    private Disposable disposable;

    private void startCounting() {
        isCounting = true;
        tvGetCode.setTextColor(getResources().getColor(R.color.color_999999));
        disposable = Observable.intervalRange(0, interval + 1, 0, 1, TimeUnit.SECONDS)
                .compose(RxUtils.io_main())
                .subscribe(aLong -> {
                    if (interval == aLong) {
                        tvGetCode.setTextColor(getResources().getColor(R.color.color_333333));
                        tvGetCode.setText("获取验证码");
                        isCounting = false;
                    } else {
                        tvGetCode.setText(getString(R.string.time_left, interval - aLong));
                    }
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
            disposable = null;
        }
    }

    @Override
    public void onError(ApiException e) {
        if (disposable != null)
            disposable.dispose();
        tvGetCode.setTextColor(getResources().getColor(R.color.color_333333));
        tvGetCode.setText("获取验证码");
        isCounting = false;
    }
}
